HiSEN

安全的重叠构造器 - 最佳实践:Build Pattern

1
effective java # 2

Builder模式,不直接生成想要的对象,而是让客户端利用所有必要的参数调用构造器,

得到一个builder对象。然后客户端在builder对象上调用类似于setter的方法,

来设置每个相关可选的参数,最后调用无参的build来生成不可变的对象。

完整代码+测试:github:完整代码+测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
public class NutritionFacts {

private final int servingSize;
private final int servings;
private final int calories;
private final int fat;
private final int sodium;
private final int carbohydrate;

private NutritionFacts(Builder builder) {
servingSize = builder.servingSize;
servings = builder.servings;
calories = builder.calories;
fat = builder.fat;
sodium = builder.sodium;
carbohydrate = builder.carbohydrate;
}

public static class Builder {

// Required parameters
private final int servingSize;
private final int servings;

private int calories = 0;
private int fat = 0;
private int carbohydrate = 0;
private int sodium = 0;

public Builder(int servingSize, int servings) {
this.servingSize = servingSize;
this.servings = servings;
}

public Builder calories(int val) {
calories = val;
return this;
}

public Builder fat(int val) {
this.fat = val;
return this;
}

public Builder carbohydrate(int val) {
this.carbohydrate = val;
return this;
}

public Builder sodium(int val) {
this.sodium = val;
return this;
}

public NutritionFacts build() {
return new NutritionFacts(this);
}
}



@Override
public String toString() {
return "NutritionFacts{" +
"servingSize=" + servingSize +
", servings=" + servings +
", calories=" + calories +
", fat=" + fat +
", sodium=" + sodium +
", carbohydrate=" + carbohydrate +
'}';
}
}